-----摇蛋-----

local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent());
local LanguageHelper= _G.LanguageHelper
local UIManager = ClientOHOH.load.UIManager();
local Egg_Cfg=ClientOHOH.config.Egg
local Goods_Cfg=ClientOHOH.config.Goods
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local GlobalTipGui =UIManager.ui_list.GlobalTipGui;
local EventList = ClientOHOH.load.EventList();
local PetTool=_G.CommonOHOH.load.PetTool();

local HatchEgg2PetGui=UIManager:CreateUI("HatchEgg2PetGui",require(script.HatchEgg2PetGui))
local ItemTool=_G.CommonOHOH.load.ItemTool()

function M.new(self,...)
    return M(self,...);
end;


function M.ctor(self, parent)
    M.super.ctor(self, parent);

    self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.player=parent
    self.RobuxBuyEvent=self.player.RobuxBuyEvent

    self.IsCanHatchShow=true --控制是否播放摇蛋动画

    self.LuckGui={}-- 展示摇蛋（宠物概率) 界面实体

    self.LuckGuiClass={}-- 展示摇蛋（宠物概率) 界面类名

    self.EggId2Mesh={} -- 蛋id--》蛋的模型

    
    self.server_hatch_data={} --来自服务器的摇蛋数据

    HatchEgg2PetGui:init(self)

    self.server_hatch_clear_event=_G.CommonOHOH.load.Connect().new()

    for  k,v in pairs(script.LuckView:GetChildren()) do
        self.LuckGuiClass[v.Name]=require(v)
    end

    --初始化摇蛋显示
    for k,v in pairs(Egg_Cfg) do

        local egg=ClientOHOH:GetPath_Loop(v.path,workspace)
        self.EggId2Mesh[k]=ClientOHOH:GetPath_Loop("Ctrl.HatchEgg",egg).Value
        local priceLable=ClientOHOH:GetPath_Loop("Ctrl.Price",egg).Value
        v["price"]=v.arg
        priceLable.Text=BigNumberHelper:ToString(v.price)

        local priceImage=ClientOHOH:GetPath_Loop("Ctrl.costIcon",egg).Value
        local priceIcon=ItemTool.GetItemShowView(v.costId)
        priceImage.Image=priceIcon
        v["priceIcon"]=priceIcon
        
        local eggName=""
        if v.name>0 then --蛋存在名字
            eggName=LanguageHelper:GetText(v.name)
        end

        local nameLable=ClientOHOH:GetPath_Loop("Ctrl.Name",egg).Value
        nameLable.Text=eggName


        local touchPart=ClientOHOH:GetPath_Loop("Ctrl.Touch",egg).Value
        local att=ClientOHOH:GetPath_Loop("Ctrl.Att",egg).Value

        local guivalue=egg.Ctrl:FindFirstChild("Gui")

        if guivalue and guivalue.Value then
           local gui= guivalue.Value
            self.LuckGui[k]=self.LuckGuiClass[gui.Name].new(v,att,self,self.parent,gui)
        else
            self.LuckGui[k]=self.LuckGuiClass.common.new(v,att,self,self.parent)
        end

        ClientOHOH.load.CallBackManager():AddRbxConnect(self,touchPart.Touched, function(obj,chPart)

           if  _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart,"Head")  then
                local luck=self.LuckGui[k]

                luck:Show()
           end

        end);

        ClientOHOH.load.CallBackManager():AddRbxConnect(self,touchPart.TouchEnded, function(obj,chPart)

            if  _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart,"Head")  then
                self.LuckGui[k]:Hide()
            end

        end);
    end
end;


function M.dtor(self)
    M.super.dtor(self);
end;

--获取蛋的模型
function M.CreateEggMesh(self,id)
    local e=self.EggId2Mesh[id]:Clone()
    return  e
end

--是否拥有某种通行证
function M.IsHadGamePass(self,id)
   return  self.DataBase:GetData("gamePass")[id]
end

local gPassId2GoodId={}
local gPassId2Value={}

for goodId,v in pairs(Goods_Cfg) do
    if v.gType=="gamePass" then
        for k,j in pairs(v.itemData) do
            gPassId2GoodId[k]=goodId
            gPassId2Value[k]=j
        end
    end
end

function M.GetGamePassGoodId(self,id)
    return gPassId2GoodId[id]
end

function M.GetGamePassValue(self,id)
    return gPassId2Value[id]
end


function M.E(self,eggId,deleteData)
    if  PetTool.IsCanPlacedPet(1,self.DataBase) then

        if not self:GetIsCanHatchShow() or  #self.server_hatch_data>0 then --正在摇蛋，放弃连点
            return 
        end

        self:Hatch(1,eggId,deleteData)
    else
        GlobalTipGui:Show(LanguageHelper:GetText(60))
    end
   
end

local g_R_gamePassId="gamepass_hatch_egg1"
local g_Y_gamePassId="gamepass_hatch_egg2"
local g_T_gamePassId="gamepass_vip"


function M.R(self,eggId,deleteData)
    local passId=g_R_gamePassId
    if self:IsHadGamePass(passId) then
        if not self:GetIsCanHatchShow() or  #self.server_hatch_data>0 then --正在摇蛋，放弃连点
            return 
        end
        local count=self:GetGamePassValue(passId)
        if PetTool.IsCanPlacedPet(count,self.DataBase) then
            self:Hatch(count,eggId,deleteData)
        else
            GlobalTipGui:Show(LanguageHelper:GetText(60))
        end
    else
        self.RobuxBuyEvent:Action(self:GetGamePassGoodId(passId))
    end
end

function M.Y(self,eggId,deleteData)
    local passId=g_Y_gamePassId
    if self:IsHadGamePass(passId) then
        if not self:GetIsCanHatchShow() or  #self.server_hatch_data>0 then --正在摇蛋，放弃连点
            return 
        end

        local count=self:GetGamePassValue(passId)
        if PetTool.IsCanPlacedPet(count,self.DataBase) then
            self:Hatch(count,eggId,deleteData)
        else
            GlobalTipGui:Show(LanguageHelper:GetText(60))
        end
    else

        self.RobuxBuyEvent:Action(self:GetGamePassGoodId(passId))
    end
end

--关闭自动摇蛋
function M.Close_T(self)
    if self.T_conn then
        self.T_conn:Disconnect()
        self.T_conn=nil
    end
end

function M.T(self,eggId,deleteData)
    local passId=g_T_gamePassId

    if self:IsHadGamePass(passId) then
        
        if self.T_conn then
            return 
        end

        -- 开始自动摇蛋
        self:AutoHatch(eggId,deleteData)

        self.T_conn=self.server_hatch_clear_event:Connect(function()
            return self:AutoHatch(eggId,deleteData)
        end)
       
    else
        self.RobuxBuyEvent:Action(self:GetGamePassGoodId(passId))
    end
end


function M.AutoHatchCount(self)
    local count=1
    if self:IsHadGamePass(g_R_gamePassId) then
        count=self:GetGamePassValue(g_R_gamePassId)
    end

    if self:IsHadGamePass(g_Y_gamePassId) then
        count=self:GetGamePassValue(g_Y_gamePassId)
    end

    local c=PetTool.GetCanPlacePet(self.DataBase)
    if c>=count then
        return count
    end
    return 0
end

function M.AutoHatch(self,eggId,deleteData)
    local count=self:AutoHatchCount()
    if count>0 then
        self:Hatch(count,eggId,deleteData)
        return true
    else
        GlobalTipGui:Show(LanguageHelper:GetText(60))
        return false
    end
end

local CostTip={
    base_gold=LanguageHelper:GetText(61),
    base_wins=LanguageHelper:GetText(6),
}

function M.Hatch(self,count,eggId,deleteData)
    local cfg=Egg_Cfg[eggId]
    -- print("eggId,count,deleteData",eggId,count,deleteData,cfg)
    local cost=count*cfg.arg
    --判断钱够不够
    if  not ItemTool.IsHadEnough(cfg.costId,cost,self.DataBase) then
        GlobalTipGui:Show(CostTip[Egg_Cfg[eggId].costId])
        return false
    end
    self:Fire(EventList.CommonHatch,eggId,count,deleteData);
end


function M.SetIsCanHatchShow(self,iscan)
    self.IsCanHatchShow=iscan
end

function M.GetIsCanHatchShow(self)
    return  self.IsCanHatchShow 
end

--收到服务器数据，开始摇蛋
function M.PlayServerHatchData(self)
 
    if self.ShowHatchConn then
        return 
    end

    if self.HatchDoneConn then
        self.HatchDoneConn:Disconnect()
        self.HatchDoneConn=nil
    end


    -- 显示孵蛋界面
    HatchEgg2PetGui:Show()

    self.isHaching=nil --是否在孵蛋中

   self.HatchDoneConn= HatchEgg2PetGui.HatchDoneEvent:Connect(function()
        self.isHaching=nil
        if #self.server_hatch_data==0   then

           local list= self.server_hatch_clear_event:Action()

           --中断结束
           for k,v in pairs(list) do
                if v then
                    return 
                end
           end

            HatchEgg2PetGui:Hide()
            if self.HatchDoneConn then
                self.HatchDoneConn:Disconnect()
                self.HatchDoneConn=nil
            end
            if self.ShowHatchConn then
                self.ShowHatchConn:Disconnect()
                self.ShowHatchConn=nil
            end
            
        end
    end)


	self.ShowHatchConn=self:AddStepUpdate(function()
        if #self.server_hatch_data>0   then
            if not self.isHaching and self:GetIsCanHatchShow()  then
                local hatchData=self.server_hatch_data[1] --取出第一个
                table.remove(self.server_hatch_data,1) --删除第一个数据
                self.isHaching=true
                HatchEgg2PetGui:HatchShow(hatchData.eggId,hatchData.petIdList)

                self:ShowDeletePetTip(hatchData)
            end
        end
	end)
end

M:RegistEvent(EventList.hatch_egg,function(self,petIdList,eggId,count,deletePetIds)
    -- print("-------hatch_egg-------------",eggId)
     local data={
        petIdList=petIdList,
        eggId=eggId,
        count=count,
        delete=deletePetIds
     }

     table.insert(self.server_hatch_data,data)


     self:PlayServerHatchData()
end)

function M.ShowDeletePetTip(self,data)
    local sum=0
    for _,v in pairs(data.petIdList) do
        if data.delete[v] then
            sum=sum+1
        end
       
    end
    local t=sum
	if t>0 then
		GlobalTipGui:Show(LanguageHelper:GetText(30016,{"%%ARG1"},{t}))
	end
end

return M